package d1123;
//修改，删除非蜀国

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Demo04ListExe {
    public static void main(String[] args) {


        Person p1 = new Person("刘备","蜀国");
        Person p2 = new Person("关羽","蜀国");
        Person p3 = new Person("张飞","蜀国");
        Person p4 = new Person("曹操","魏国");
        Person p5 = new Person("孙权","吴国");
        List <Person>list =new ArrayList();
        Collections.addAll(list,p1,p2,p3,p4,p5);
       //有条件的删除和修改  非蜀国人删除掉
      /*  for (Person p  :list){
           Person p =(Person )o;
            if(!p.getCountry().equals("蜀国")){
                list.remove(p);
            }
        }
       System.out.println(list);
        //使用迭代器删除
      /*  Iterator <Person> it=list.iterator();
        while (it.hasNext()){
            Person p=it.next();
            if(!p.getCountry().equals("蜀国")){
                //list.remove(p)
                it.remove();

        }
    }
        System.out.println(list);*/
       //forEach();
      /*  list.forEach(new Consumer<Person>() {
            @Override
            public void accept(Person person) {
                if (person.getCountry().equals("蜀国"));
                list.remove(person );

            }
        });*/
        //传统for
        for (int i=0;i<list.size();i++){
            Person p=list.get(i);
            if(!p.getCountry().equals("蜀国")) {
              //  list.remove(i); 有bug
                list.remove(i--);
            }
        }System.out.println(list);
    }
        }
